package com.ruoyi.womp.ws.impl;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.womp.domain.WompWeighingApparatus;
import com.ruoyi.womp.domain.WompWeighingApparatusElement;
import com.ruoyi.womp.service.ISqlQueryService;
import com.ruoyi.womp.service.IWompWeighingApparatusElementService;
import com.ruoyi.womp.service.IWompWeighingApparatusService;
import com.ruoyi.womp.ws.DeviceRegWebService;

@Service
@WebService(serviceName = "DeviceRegWebService", targetNamespace = "http://ws.cdwy.com", endpointInterface = "com.ruoyi.womp.ws.DeviceRegWebService")
public class DeviceRegWebServiceImpl implements DeviceRegWebService {

	@Autowired
	IWompWeighingApparatusService weighingApparatusService;
	@Autowired
	IWompWeighingApparatusElementService weighingApparatusElementService;
	@Autowired
	ISqlQueryService sqlQueryService;

	@Override
	public String deviceReg(String deviceData) {
		System.out.println("设备注册：" + deviceData);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("code", 0);
		resultMap.put("message", "success");

		if (null != deviceData && deviceData.length() > 0) {
			try {
				JSONObject deviceDataJsonObject = JSONObject.parseObject(deviceData);
				doDeviceReg(deviceDataJsonObject);
			} catch (Exception e) {
				e.printStackTrace();
				resultMap.put("code", 9);
				resultMap.put("message", "data format error:" + e.getMessage());
			}
		} else {
			resultMap.put("code", 10);
			resultMap.put("message", "data empty error");
		}

		return JSONObject.toJSONString(resultMap);
	}

	@Transactional
	private void doDeviceReg(JSONObject deviceDataJsonObject) {
		JSONObject messageJsonObject = deviceDataJsonObject.getJSONObject("message");

		String userId = messageJsonObject.getString("userId");
		String watype = messageJsonObject.getString("type");
		String wacode = messageJsonObject.getString("code");
		String waname = messageJsonObject.getString("name");
		String wamodel = messageJsonObject.getString("model");

		// 删除数据
		String sql = "delete from womp_weighing_apparatus_element where wa_code='" + wacode + "'";
		sqlQueryService.executeSql(sql);

		// 查询原数据，更新后保存
		// QueryWrapper<WompWeighingApparatus> queryWrapper = new
		// QueryWrapper<WompWeighingApparatus>().eq("code", wacode);
		WompWeighingApparatus weighingApparatus = new WompWeighingApparatus();

		sql = "select * from womp_weighing_apparatus where code='" + wacode + "'";
		Map<String, Object> weighingApparatusMap = sqlQueryService.queryOne(sql);

		if (null == weighingApparatusMap) {
			weighingApparatus = new WompWeighingApparatus();
			// 存储数据
			weighingApparatus.setUserId(userId);
			weighingApparatus.setType(watype);
			weighingApparatus.setCode(wacode);
			weighingApparatus.setName(waname);
			weighingApparatus.setModel(wamodel);
			weighingApparatus.setUpdateTime(Calendar.getInstance().getTime());
			weighingApparatusService.insertWompWeighingApparatus(weighingApparatus);
		} else {
			JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(weighingApparatusMap));
			weighingApparatus = JSONObject.toJavaObject(jsonObject, WompWeighingApparatus.class);
			// 存储数据
			weighingApparatus.setUserId(userId);
			weighingApparatus.setType(watype);
			weighingApparatus.setCode(wacode);
			weighingApparatus.setName(waname);
			weighingApparatus.setModel(wamodel);
			weighingApparatus.setUpdateTime(Calendar.getInstance().getTime());
			weighingApparatusService.updateWompWeighingApparatus(weighingApparatus);
		}

		JSONArray elementJsonArray = messageJsonObject.getJSONArray("elements");
		for (Object object : elementJsonArray) {
			JSONObject elementJsonObject = JSONObject.parseObject(object.toString());
			String elementCode = elementJsonObject.getString("code");
			Integer orderNum = elementJsonObject.getInteger("orderNum");

			WompWeighingApparatusElement weighingApparatusElement = new WompWeighingApparatusElement();
			weighingApparatusElement.setWaCode(wacode);
			weighingApparatusElement.setElementCode(elementCode);
			weighingApparatusElement.setOrderNum(orderNum);
			weighingApparatusElement.setCreateTime(Calendar.getInstance().getTime());

			weighingApparatusElementService.insertWompWeighingApparatusElement(weighingApparatusElement);
		}
	}
}
